package org.gap.wizards.world.builder;

import java.awt.Point;
import java.util.Random;

import org.gap.jseed.SeedContainer;
import org.gap.wizards.Start;
import org.gap.wizards.creature.AnimalBrain;
import org.gap.wizards.creature.AnimalFactory;
import org.gap.wizards.creature.Creature;
import org.gap.wizards.items.Item;
import org.gap.wizards.items.ItemFactory;
import org.gap.wizards.world.Dungeon;
import org.gap.wizards.world.Location;
import org.gap.wizards.world.MapContainer;
import org.gap.wizards.world.MapTransition;
import org.gap.wizards.world.MapTransition.Direction;
import org.gap.wizards.world.Wilderness;

public class WorldBuilder {

	private static char[][] worldSeed = new char[][] { { 'c', 'c', 'c', 'c' }, { 'c', 'f', 'f', 'c' }, { 'c', 'f', 'f', 'c' }, { 'c', 'c', 'c', 'c' } };

	public static void createWorld(SeedContainer store) {
		MapContainer container = store.get(MapContainer.class);
		for (int x = 0; x < worldSeed.length; x++) {
			for (int y = 0; y < worldSeed[0].length; y++) {
				if (worldSeed[x][y] == 'f') {
					Wilderness location = null;
					if (new Random().nextInt(6) > 3) {
						location = WildernessBuilder.createDenseForest();
					} else {
						location = WildernessBuilder.createLightForest(store);
					}
					container.addMap(createMapId(x, y), location);
					addAutoTransitions(location, x, y);
				} else if (worldSeed[x][y] == 'c') {
					Wilderness location = WildernessBuilder.createDenseForest();
					container.addMap(createMapId(x, y), location);
					addAutoTransitions(location, x, y);
				}
			}
		}
		container.startWithMap("map-1-1");
	}

	private static void addAutoTransitions(Wilderness location, int x, int y) {
		int middleY = Start.SCREEN_HEIGHT / 2;
		int middleX = Start.SCREEN_WIDTH / 2;

		if (x - 1 >= 0) {
			location.addTransition(new MapTransition(Direction.Over, new Point(0, middleY), createMapId(x - 1, y), new Point(Start.SCREEN_WIDTH - 1, middleY)));
		}
		if (x + 1 < worldSeed.length) {
			location.addTransition(new MapTransition(Direction.Over, new Point(Start.SCREEN_WIDTH - 1, middleY), createMapId(x + 1, y), new Point(0, middleY)));
		}
		if (y - 1 >= 0) {
			location.addTransition(new MapTransition(Direction.Over, new Point(middleX, 0), createMapId(x, y - 1), new Point(middleX, Start.SCREEN_HEIGHT - 1)));
		}
		if (y + 1 < worldSeed[0].length) {
			location.addTransition(new MapTransition(Direction.Over, new Point(middleX, Start.SCREEN_HEIGHT - 1), createMapId(x, y + 1), new Point(middleX, 0)));
		}
	}

	private static String createMapId(int x, int y) {
		return "map-" + x + "-" + y;
	}

	private static Location dungeon(SeedContainer store) {
		Dungeon dungeon = new Dungeon();
		dungeon.addPath(new Point(5, 5), new Point(10, 5));
		dungeon.addPath(new Point(10, 5), new Point(10, 15));
		dungeon.addClosedSpace(6, 6, 4, 6, new Point(8, 6));
		dungeon.addClosedSpace(8, 16, 8, 7, new Point(10, 16));
		dungeon.addTransition(new MapTransition(Direction.Up, new Point(5, 5), "wilderness-1", new Point(51, 12)));

		Creature dog1 = AnimalFactory.createDog(store.create(AnimalBrain.class));
		dog1.setLocation(new Point(11, 17));
		Creature dog2 = AnimalFactory.createDog(store.create(AnimalBrain.class));
		dog2.setLocation(new Point(12, 17));
		dungeon.addCreature(dog1);
		dungeon.addCreature(dog2);
		Item item = ItemFactory.healingElixerInstructions();
		dungeon.addItemAt(new Point(12, 18), item);
		return dungeon;
	}

}
